package pl.edu.agh.ki.ewywiadowka.client.windows.educator;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.dto.MarkDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.StudentDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.SubjectDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.TeacherDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;

public class FinalMarksWindow extends GenericWindow implements ClickHandler {

	private VerticalPanel _mainPanel = new VerticalPanel();
	private HorizontalPanel _hPanel = new HorizontalPanel();

	private StudentDTO _student = null;
	private SubjectDTO _subject = null;

	private Button _removeButton = new Button("Usun ocene");
	private Grid _setMarkGrid = new Grid(2, 2);
	private HorizontalPanel _markHPanel = new HorizontalPanel();
	private ListBox _valueListBox = new ListBox();
	private DateBox _dateBox = new DateBox();
	private Button _setButton = new Button("Ustal ocene");

	private Grid _barGrid = new Grid(3, 2);

	private TeacherDTO _teacher = null;
	private MarkDTO _finalMark = null;

	private int _mode = -1;

	public FinalMarksWindow(EWywiadowka wywiadowka, StudentDTO student,
			SubjectDTO subject, int mode) {
		super(wywiadowka);

		_mode = mode;
		_student = student;
		_subject = subject;

		_removeButton.addClickHandler(this);
		_setButton.addClickHandler(this);

		_barGrid.setWidget(0, 0, new Label("Uczeń: "));
		_barGrid.setWidget(0, 1, new Label(student.toString() + "; "
				+ student.clas.toString()));
		_barGrid.setWidget(1, 0, new Label("Przedmiot: "));
		_barGrid.setWidget(1, 1, new Label(subject.toString()));
		_barGrid.setWidget(2, 0, new Label("Nauczyciel: "));

		Const.eWywiadowkaService.getSubjectTeacherForClass(subject, student,
				new AsyncCallback<TeacherDTO>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(TeacherDTO result) {
						if (result != null) {
							_teacher = result;
							_barGrid.setWidget(2, 1, new Label(_teacher
									.toString()));
						}
					}
				});

		_hPanel.add(new Label("Ocena koncowa: "));

		for (Integer i = 1; i <= 6.0; i++)
			_valueListBox.addItem(i.toString());

		_setMarkGrid.setWidget(0, 0, new Label("Ocena"));
		_setMarkGrid.setWidget(1, 0, _valueListBox);
		_setMarkGrid.setWidget(0, 1, new Label("Data"));
		_setMarkGrid.setWidget(1, 1, _dateBox);

		_markHPanel.add(_setMarkGrid);
		_markHPanel.add(_setButton);

		_mainPanel.add(_barGrid);
		_mainPanel.add(_hPanel);

		Const.eWywiadowkaService.getFinalMark(_student, _subject,
				new AsyncCallback<MarkDTO>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(MarkDTO result) {
						_finalMark = result;
						_hPanel.add(new Label(result == null ? "brak" : result
								.toString()));

						if (_mode == 1)
							if (result == null)
								_mainPanel.add(_markHPanel);
							else
								_mainPanel.add(_removeButton);
					}

				});

		this.initWidget(_mainPanel);

	}

	@Override
	public void onClick(ClickEvent event) {
		if (event.getSource() == _removeButton) {
			Const.eWywiadowkaService.removeFinalMark(_finalMark,
					new AsyncCallback<Boolean>() {

						@Override
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage());
						}

						@Override
						public void onSuccess(Boolean result) {
							Window.alert(result ? "Usunieto" : "Blad usuwania");
							getWywiadowka().putWindow(
									new FinalMarksWindow(getWywiadowka(),
											_student, _subject, 1));
						}

					});
		} else if (event.getSource() == _setButton) {
			if (_dateBox.getValue() == null)
				Window.alert("Prosze ustawic poprawna date");
			else {
				final MarkDTO mark = new MarkDTO(null, Integer
						.parseInt(_valueListBox.getValue(_valueListBox
								.getSelectedIndex())), _dateBox.getValue(),
						null, _student, _subject);

				Const.eWywiadowkaService.setFinalMark(mark, _teacher,
						new AsyncCallback<Boolean>() {

							@Override
							public void onFailure(Throwable caught) {
								Window.alert(caught.getMessage());
							}

							@Override
							public void onSuccess(Boolean result) {
								Window.alert(result ? "Ocena zatwierdzona"
										: "Blad dodawania");
								getWywiadowka().putWindow(
										new FinalMarksWindow(getWywiadowka(),
												_student, _subject, 1));
							}

						});
			}

		}
	}

}
